package com.briup.cms.dao;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ArticleMapper {
    @Results(id = "ArticleMapper", value = {
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "readTimes", column = "read_times"),
            @Result(property = "thumbUpTimes", column = "thumb_up_times"),
            @Result(property = "thumbDownTimes", column = "thumb_down_times"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "userId", column = "user_id")
    })
    @Select("SELECT * FROM cms_article")
    List<Article> selectAll();

    @Results(id = "ArticleExtend", value = {
            @Result(property = "publishDate", column = "publish_date"),
            @Result(property = "readTimes", column = "read_times"),
            @Result(property = "thumbUpTimes", column = "thumb_up_times"),
            @Result(property = "thumbDownTimes", column = "thumb_down_times"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "categoryName", column = "category_id")
    })
    @Select("SELECT a.*, u.username FROM (SELECT a.*, c.name AS category_name FROM cms_article a " +
            "LEFT JOIN cms_category c ON a.category_id = c.id) AS a " +
            "LEFT JOIN cms_user AS u ON a.user_id = u.id;")
    List<ArticleExtend> selectAllWithName();

    @Insert("INSERT INTO cms_article VALUES(" +
            "NULL, #{title}, #{content}, #{publishDate}, #{readTimes}, #{thumbUpTimes}, #{thumbDownTimes}," +
            "#{status}, #{categoryId}, #{userId}" +
            ")")
    void insert(Article article);

    @Update("UPDATE cms_article SET " +
            "title=#{title}, content=#{content}, publish_date=#{publishDate}, read_times=#{readTimes}," +
            "thumb_up_times=#{thumbUpTimes}, thumb_down_times=#{thumbDownTimes}," +
            "status=#{status}, category_id=#{categoryId}, user_id=#{userId} " +
            "WHERE id=#{id}")
    void update(Article article);

    @Delete("DELETE FROM cms_article WHERE id=#{id}")
    void deleteById(Long id);
}
